 #include "playpage.h"
#include "ui_playpage.h"
#include<QPainter>
#include<QPixmap>
#include<QIcon>
#include<QTimer>
#include"runner.h"
#include"obstacle.h"
#include<QDebug>
#include<QMediaPlayer>
#include<QMediaPlaylist>
#include<QLabel>
int playpage::x1=0;//设置static变量的初始值
int playpage::x2=1000;
int playpage::y=550;
int playpage::index=0;
int playpage::score=0;
playpage::playpage(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::playpage)
{
    ui->setupUi(this);
    QLabel* label1=new QLabel(this);
    QLabel* label2=new QLabel(this);
    label1->setText(QString::number(score));
    label2->setText(QString::number(score));
    label1->show();
    label2->hide();
    setFixedSize(1000,700);
    setWindowTitle("Stick Runner");//窗口标题
    setWindowIcon(QIcon(":/image/Image/title.png"));//贴窗口的logo图
    for(int i=0;i<20;i++){//初始化障碍物数组中的成员
        barrier1[i].initimage1();
    }
    for(int i=0;i<20;i++){
        barrier2[i].initimage2();
    }
    timer->start(80);//定时器参数设定
    over=false;
    difficulty=0;
    groundy=650;
    connect(timer,&QTimer::timeout,[=](){//让地面和背景随着时间移动
        if(x1==-1000) x1=1000;
        if(x2==-1000) x2=1000;
        x1-=20;
        x2-=20;
        if(y<550){
            y+=25;
        }
        update();
    });
    connect(timer,&QTimer::timeout,[=](){
        if(index==4) index=0;
       index++;
       if(difficulty<=70){//难度随时间增加
           difficulty+=0.002;
       }
       score+=100;
       addObstacle();//添加障碍物
       for(int i=0;i<20;i++){
           if(!barrier1[i].free){//若障碍物不在闲置状态，判断其位置，若在屏幕外则让其变成闲置状态
               barrier1[i].befree();
           }
       }
       for(int i=0;i<20;i++){
           if(!barrier2[i].free){
               barrier2[i].befree();
           }
       }
       update();
       for(int i=0;i<20;i++){//让障碍物移动，并判断是否与人物相撞，相撞则游戏结束
           barrier1[i].move();
           if(barrier1[i].isCollision(y)) over=true;
           break;
       }
       for(int i=0;i<20;i++){
           barrier2[i].move();
           if(barrier2[i].isCollision(y)) over=true;
           break;
       }
    });
    if(over) {
        timer->stop();
        label1->hide();
        label2->show();
    }
}
void playpage::paintEvent(QPaintEvent *){
    QPainter painter(this);
    bg1.load(":/background/Image/backg.jpg");//加载背景和地面
    bg2.load(":/background/Image/backg.jpg");
    gd1.load(":/background/Image/ground.png");
    gd2.load(":/background/Image/ground.png");
    painter.drawPixmap(x1,0,1000,700,bg1);//跑步时的背景
    painter.drawPixmap(x2,0,1000,700,bg2);
    painter.drawPixmap(QRect(x1,650,1000,50),gd1);//画地面
    painter.drawPixmap(QRect(x2,650,1000,50),gd2);
    QRect q1(0,0,0,0);
    QRect q2(40,y,80,100);
    if(y==550){
    painter.drawPixmap(q2,hero.run[index],q1);//画人物和动作
    }
    if(y<550){
    painter.drawPixmap(q2,hero.jump,q1);
    }
    for(int i=0;i<20;i++)
    {
        if(!barrier1[i].free){//障碍物被释放出来后就画
            painter.drawPixmap(QRect(barrier1[i].getX(),barrier1[i].getY(),barrier1[i].getWidth(),barrier1[i].getHeight()),barrier1[i].getImg());
            update();
            break;
        }
    }
    for(int i=0;i<20;i++){
        if(!barrier2[i].free){
            painter.drawPixmap(QRect(barrier2[i].getX(),barrier2[i].getY(),barrier2[i].getWidth(),barrier2[i].getHeight()),barrier2[i].getImg());
            update();
            break;
        }
    }
    if(over){//画游戏结束的封面
        painter.drawPixmap(0,0,1000,700,QPixmap(":/image/Image/gameover.jpg"));
    }
    update();
}
void playpage::keyPressEvent(QKeyEvent *event){//按K键跳跃
    switch(event->key()){
    case Qt::Key_K:{
        if(y==550) y-=300;
        if(250<=y&&y<550) y-=200;
        break;
    }
    }
}
void playpage::gameover(){
    over=true;
}
void playpage::addObstacle(){
    for(int i=0;i<20;i++){
        if(barrier1[i].free){//第一种障碍物从下往上长
            srand(time(NULL));
            int x=1000;
            int h=(rand()%101)+100;
            int y1=650-h;
            int w=10;
            barrier1[i].initWall(x,y1,w,h);
            barrier1[i].free=false;
            break;
        }
    }
    for(int i=0;i<20;i++){//第二种障碍物从上往下长
        if(barrier2[i].free){
            barrier2[i].initimage2();
            srand(time(NULL));
            int x=1000;
            int h=rand()%200;
            int y1=0;
            int w=30;
            barrier2[i].initWall(x,y1,w,h);
            barrier2[i].free=false;
            break;
        }
    }
}
playpage::~playpage()
{
    delete ui;
}
